import tkinter as tk
import tkinter.font as tkfont
from datetime import datetime, timedelta
demand1 = "计算时间差值"
demand2 = "计算结束时间"
demand3 = "计算起始时间"

class TimeCalculatorGUI:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("时间计算器")
        self.root.geometry("600x500")

        # 设置字体
        self.font = tkfont.Font(size=20)

        # 创建用户需求选择下拉框
        self.demand_label = tk.Label(self.root, text="请选择需求：", font=self.font)
        self.demand_label.pack()
        self.demand_var = tk.StringVar()
        self.demand_var.set("请选择")
        self.demand_dropdown = tk.OptionMenu(self.root, self.demand_var, demand1, demand2, demand3, command=self.on_demand_change)
        self.demand_dropdown.config(font=self.font)
        self.demand_dropdown.pack()

        # 创建起始时间输入框
        self.start_label = tk.Label(self.root, text="起始时间（YYYYMMDD-HHMMSS）：", font=self.font)
        self.start_label.pack()
        self.start_entry = tk.Entry(self.root, font=self.font)
        self.start_entry.pack()

        # 创建结束时间输入框
        self.end_label = tk.Label(self.root, text="结束时间（YYYYMMDD-HHMMSS）：", font=self.font)
        self.end_label.pack()
        self.end_entry = tk.Entry(self.root, font=self.font)
        self.end_entry.pack()

        # 创建时间差值输入框
        self.time_diff_label = tk.Label(self.root, text="时间差值：", font=self.font)
        self.time_diff_label.pack()
        self.time_diff_entry = tk.Entry(self.root, state=tk.DISABLED, font=self.font)
        self.time_diff_entry.pack()

        # 创建时间差值单位选择框
        self.unit_label = tk.Label(self.root, text="时间差值单位：", font=self.font)
        self.unit_label.pack()
        self.unit_var = tk.StringVar()
        self.unit_var.set("秒")
        self.unit_dropdown = tk.OptionMenu(self.root, self.unit_var, "秒", "分", "时", command=self.on_unit_change)
        self.unit_dropdown.config(font=self.font)
        self.unit_dropdown.pack()

        # 创建计算按钮
        self.calculate_button = tk.Button(self.root, text="计算", command=self.calculate, font=self.font)
        self.calculate_button.pack()

        # 创建结果显示框
        self.result_label = tk.Label(self.root, text="", font=self.font, fg="red", padx=10, pady=10)
        self.result_label.pack()

    def on_demand_change(self, event):
        demand = self.demand_var.get()
        if demand == demand1:
            self.start_entry.config(state=tk.NORMAL)
            self.end_entry.config(state=tk.NORMAL)
            self.time_diff_entry.config(state=tk.DISABLED)
            self.time_diff_entry.delete(0, tk.END)
        elif demand == demand2:
            self.start_entry.config(state=tk.NORMAL)
            self.end_entry.config(state=tk.DISABLED)
            self.end_entry.delete(0, tk.END)
            self.time_diff_entry.config(state=tk.NORMAL)
        elif demand == demand3:
            self.start_entry.config(state=tk.DISABLED)
            self.start_entry.delete(0, tk.END)
            self.end_entry.config(state=tk.NORMAL)
            self.time_diff_entry.config(state=tk.NORMAL)

    def on_unit_change(self, event):
        unit = self.unit_var.get()
        if unit == "秒":
            self.time_diff_entry.config(state=tk.DISABLED)
            self.time_diff_entry.delete(0, tk.END)
        else:
            self.time_diff_entry.config(state=tk.NORMAL)

    def calculate(self):
        demand = self.demand_var.get()
        start_time = self.start_entry.get()
        end_time = self.end_entry.get()
        time_diff = self.time_diff_entry.get()
        unit = self.unit_var.get()

        if unit == "分":
            time_diff = int(time_diff) * 60
        elif unit == "时":
            time_diff = int(time_diff) * 3600
        if demand == demand1:
            if not start_time or not end_time:
                self.result_label.config(text="起始时间和结束时间为必填项")
                return
            time_difference = self.calculate_time_difference(start_time, end_time)
            minutes = self.convert_to_minutes(time_difference)
            hours = self.convert_to_hours(time_difference)
            self.result_label.config(text=f"时间差值（秒）：{time_difference}\n时间差值（分钟）：{minutes}\n时间差值（小时）：{hours}", font=self.font)
        elif demand == demand2:
            if not start_time or not time_diff:
                self.result_label.config(text="起始时间和时间差值为必填项")
                return
            time_difference = int(time_diff)
            end_time = self.calculate_end_time(start_time, time_difference)
            self.result_label.config(text=f"结束时间：{end_time}", font=self.font)
        elif demand == demand3:
            if not end_time or not time_diff:
                self.result_label.config(text="结束时间和时间差值为必填项")
                return
            time_difference = int(time_diff)
            start_time = self.calculate_start_time(end_time, time_difference)
            self.result_label.config(text=f"起始时间：{start_time}")

    @staticmethod
    def calculate_time_difference(start_time, end_time):
        start_datetime = datetime.strptime(start_time, "%Y%m%d-%H%M%S")
        end_datetime = datetime.strptime(end_time, "%Y%m%d-%H%M%S")
        time_difference = (end_datetime - start_datetime).total_seconds()
        return time_difference

    @staticmethod
    def convert_to_minutes(time_difference):
        minutes = time_difference / 60
        return round(minutes, 4)

    @staticmethod
    def convert_to_hours(time_difference):
        hours = time_difference / 3600
        return round(hours, 4)

    @staticmethod
    def calculate_end_time(start_time, time_difference):
        start_datetime = datetime.strptime(start_time, "%Y%m%d-%H%M%S")
        end_datetime = start_datetime + timedelta(seconds=time_difference)
        end_time = end_datetime.strftime("%Y%m%d-%H%M%S")
        return end_time

    @staticmethod
    def calculate_start_time(end_time, time_difference):
        end_datetime = datetime.strptime(end_time, "%Y%m%d-%H%M%S")
        start_datetime = end_datetime - timedelta(seconds=time_difference)
        start_time = start_datetime.strftime("%Y%m%d-%H%M%S")
        return start_time

    def run(self):
        self.root.mainloop()

calculator_gui = TimeCalculatorGUI()
calculator_gui.run()
